﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using System.Threading;
using ARR.UI.EnvoyImporterService;
using ARR.UI.ArticlesService;
using DDF.Entity;
using ARR.UI.Models;
using System.ServiceModel;
using System.Windows;
using ARR.Shared;
using ARR.UI.AuditTrailsService;
using ARR.UI.CategoriesService;

namespace ARR.UI.Models
{
    public interface IArticlesImporterModel : IModelBase
    {

        List<Category> Categories { get; set; }

        List<CategoryRole> CategoryRoles { get; set; }

        List<CategoryUniverse> CategoryUniverses { get; set; }

        List<ImporterResult> Results { get; set; }

        ObservableCollection<TempArticle> TempArticles { get; set; }

        ObservableCollection<TempArticle> TempArticlesCache { get; set; }


        
        List<TempArticle> GetSameStoreArticles(int storeId);
        
        void SaveCategoryMatches(List<CategoryMatch> matches);

        void SaveTempArticles();

        void UpdateSales(List<TempArticle> articlesToBeRemoved);

        void UpdateTempArticleSalesArticleIds();

        void UpdateTempArticlesCategoryMatch(int categoryId, string[] originalCategories);
    }

    public class ArticlesImporterModel : ModelBase, IArticlesImporterModel
    {

        public ArticlesImporterModel()
        {
            this.TempArticles = new ObservableCollection<TempArticle>();
            this.Results = new List<ImporterResult>();

            //if (!WorkspaceData.Instance.Country.IsArticleMasterSeparateFile)
            //{
            //    LoadUnifiedImporterResult();
            //}

            LoadCategories();
        }



        public List<Category> Categories { get; set; }

        public List<CategoryRole> CategoryRoles { get; set; }

        public List<CategoryUniverse> CategoryUniverses { get; set; }

        public RangeReview RangeReview { get; set; }

        public List<ImporterResult> Results { get; set; }

        public ObservableCollection<TempArticle> TempArticles { get; set; }

        public ObservableCollection<TempArticle> TempArticlesCache { get; set; }


        
        public List<TempArticle> GetSameStoreArticles(int storeId)
        {
            ArticlesServiceClient service = new ArticlesServiceClient();
            
            List<TempArticle> articles = new List<TempArticle>();

            try
            {
                articles = service.GetSameStoreArticles(storeId);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
            
            return articles;
        }
        
        public void SaveCategoryMatches(List<CategoryMatch> matches)
        {
            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                service.SaveCategoryMatches(matches);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void SaveTempArticles()
        {
            var modifieds = TempArticles.Where(tmp => tmp.GetEntityState() == EntityState.Modified).ToList();

            if (modifieds.Any())
            {
                ArticlesServiceClient service = new ArticlesServiceClient();

                try
                {
                    service.SaveTempArticles(modifieds);

                    var ids = Results.Select(r => r.ItemId).Where(r => r != 0).ToList();
                    TempArticlesCache = new ObservableCollection<TempArticle>(service.GetTempArticlesByIds(ids));
                    TempArticles = TempArticlesCache;

                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }
            }
        }

        public void UpdateSales(List<TempArticle> articlesWithSameName)
        {
            ArticlesServiceClient service = new ArticlesServiceClient();

            try
            {
                service.SaveTempArticleSales(articlesWithSameName);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void UpdateTempArticleSalesArticleIds()
        {
            ArticlesServiceClient service = new ArticlesServiceClient();

            try
            {
                service.UpdateTempArticleSalesArticleIds(WorkspaceData.Instance.RangeReview.Id);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void UpdateTempArticlesCategoryMatch(int categoryId, string[] originalCategories)
        {
            ArticlesServiceClient service = new ArticlesServiceClient();

            try
            {
                service.UpdateTempArticlesWithCategoryMatch(categoryId, originalCategories.ToList());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }


        private void LoadCategories()
        {
            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                var categories = service.GetByLevels(2);
                foreach (var category in categories.Where(x => x.Level == 2))
                {
                    category.SubCategories = new List<Category>();
                    category.SubCategories.AddRange(categories.Where(x => x.ParentId == category.Id));
                }
                Categories = categories.Where(x => x.Level == 2).OrderBy(x => x.Name).ToList();
                CategoryRoles = service.GetCategoryRoles();
                CategoryUniverses = service.GetCategoryUniverses();

                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }
    }


    public class ArticlesImporterModelMock : MockModelBase, IArticlesImporterModel
    {

        public ArticlesImporterModelMock()
        {
            this.TempArticles = new ObservableCollection<TempArticle>();
            this.Results = new List<ImporterResult>();

            RangeReview = new RangeReview
            {
                Title = "Range Review 1",
                CountryId = 23,
                FirstMonth = new DateTime(2010, 05, 1),
                TotalMonths = 12,
                TargetCurrencyId = 5
            };
        }



        public List<Category> Categories { get; set; }

        public List<CategoryRole> CategoryRoles { get; set; }

        public List<CategoryUniverse> CategoryUniverses { get; set; }

        public RangeReview RangeReview { get; set; }

        public List<ImporterResult> Results { get; set; }

        public ObservableCollection<TempArticle> TempArticles { get; set; }

        public ObservableCollection<TempArticle> TempArticlesCache { get; set; }



        
        public List<TempArticle> GetSameStoreArticles(int storeId)
        {
            return new List<TempArticle>();
        }

        public void RunStep1()
        {
            Results = new List<ImporterResult>();
        }

        public void RunStep2()
        {
            Results = new List<ImporterResult>();
        }

        public void RunStep3()
        {
            Results = new List<ImporterResult>();
        }

        public void RunStep4()
        {
            Results = new List<ImporterResult>();
        }
        
        public void SaveCategoryMatches(List<CategoryMatch> matches)
        {
        }

        public void SaveTempArticles()
        {

        }

        public void UpdateSales(List<TempArticle> articlesToBeRemoved)
        {
        }

        public void UpdateTempArticleSalesArticleIds()
        {
        }

        public void UpdateTempArticlesCategoryMatch(int categoryId, string[] originalCategories)
        {
        }
    }
}
